// pages/yiyuan/index.js
const DB = wx.cloud.database()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        fenleitype: ['待审核', "已审核"],
        pdtype:'待审核'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        DB.collection('users').where({
                type: '待审核',
                xinwentype: '医院'
            })
            .get()
            .then((res) => {
                console.log(res)
                this.setData({
                    list: res.data,
                })
            })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },
    onChange(e) {

        DB.collection('users').where({
                type: e.detail.title,
                xinwentype: '医院'
            })
            .get()
            .then((res) => {
                console.log(res)
                this.setData({
                    pdtype: e.detail.title,
                    list: res.data,
                })
            })
    },
    shanchu(e) {
        let that = this;
        wx.showModal({
            title: '温馨提示',
            content: '是否删除？',
            confirmText: "是",
            cancelText: "否",
            success(res) {
                if (res.confirm) {
                    wx.showLoading({
                        title: '删除中',
                    })
                    wx.cloud.callFunction({
                        name: "update",
                        data: {
                            action: "shanchuyonghu",
                            id: e.currentTarget.dataset.id,
                        }
                    }).then(res => {
                        wx.hideLoading({
                            success: (res) => {},
                        })
                        DB.collection('users').where({
                                type: that.data.pdtype,
                                xinwentype: '医院'
                            })
                            .get()
                            .then((res) => {
                                console.log(res)
                                that.setData({
                                    list: res.data,
                                })
                            })
                    })
                } else if (res.cancel) {
                }
            }
        })
    },

    shenhe(e) {
        let that = this;
        wx.showModal({
            title: '温馨提示',
            content: '是否通过？',
            confirmText: "是",
            cancelText: "否",
            success(res) {
                if (res.confirm) {
                    wx.showLoading({
                        title: '通过中',
                    })

                    wx.cloud.callFunction({
                        name: "update",
                        data: {
                            action: "shenhe",
                            id: e.currentTarget.dataset.id,
                        }
                    }).then(res => {
                        wx.hideLoading({
                            success: (res) => {},
                        })

                        DB.collection('users').where({
                                type: that.data.pdtype,
                                xinwentype: '医院'
                            })
                            .get()
                            .then((res) => {
                                console.log(res)
                                that.setData({

                                    list: res.data,
                                })
                            })


                    })


                } else if (res.cancel) {


                }
            }
        })
    },


    preview(e) {
        console.log(e);
        wx.previewImage({
            current: e.currentTarget.dataset.image, // 当前显示图片的http链接
            urls: e.currentTarget.dataset.a// 需要预览的图片http链接列表
          })
    }
})